// compoments/postItem/postItem.js
import { Topic } from "../../model/Topic.js";
import { Utils } from "../../utils/util";
import { User } from "../../model/User.js";
const topic = new Topic();
const utils = new Utils();
const user = new User();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        topic: {
            type: Object,
            value: {}
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        images: [],
        videos: [],
        voteId: [],
        showState:'',
        conDesc:''
    },
    lifetimes: {
        attached() {
            if (this.data.topic.Images != "") {
                let imagesArr = this.data.topic.Images.split(",");
                this.setData({
                    images: imagesArr
                });
            }

            if (this.data.topic.Videos != "") {
                let videoArr = this.data.topic.Videos.split(",");
                this.setData({
                    videos: videoArr
                });
            }
            if (this.data.topic.Content){
              let conDesc = this.data.topic.Content.substr(0, 200);
              let state = this.data.topic.Content.length > 200 ? "more" : "nomore";
              this.setData({
                conDesc: conDesc,
                showState: state
              });
            }
            
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        /**
         * 跳转详情
         */
        gotoDetail() {
            wx.navigateTo({
                url: `/pages/detail/index?id=${this.data.topic['PostId']}`
            });
        },
        //修改显示状态
        changeState(){
            let state = this.data.showState=="more"?"all":"more";
            this.setData({
                showState:state
            });
        },
        /**
         * 点赞、取消点赞
         */
        thumb() {
            const ty = this.data.topic['LoggerIsLike'] == 0 ? "like" : "unlike"
            let options = {
                type: ty,
                id: this.data.topic['PostId']
            }
            topic.like(options)
                .then(res => {
                    this.triggerEvent("thumb", {
                        type: ty,
                        id: this.data.topic['PostId']
                    });
                })
                .catch(err => {
                    wx.showToast({
                        title: err.msg,
                        icon: "none"
                    })
                });
        },
        /**
         * 关注、取关
         */
        follow() {
            const ty = this.data.topic['LoggerIsFollow'] == 0 ? "follow" : "unfollow";
            let options = {
                type: ty,
                id: this.data.topic['PostId']
            }
            topic.follow(options)
                .then(res => {
                    this.triggerEvent("follow", {
                        id: this.data.topic['PostId'],
                        type: ty
                    });
                })
                .catch(err => {
                    wx.showToast({
                        title: err.msg,
                        icon: "none"
                    })
                });
        },
        /**
         * 视频播放
         */
        playVideo(e) {
            const url = e.currentTarget.dataset.url;
            wx.navigateTo({
                url: `/pages/video/index?url=${url}`
            });
        },
        /**
         * 预览图片
         */
        previewImg(e) {
            const index = e.currentTarget.dataset.index;
            utils.previewImage(index, this.data.images);
        },
        /**
         * 举报
         */
        report() {
            let itemList = ["恶意引战", "劣质广告", "日经贴, 重复太多", "拒绝黄赌毒", "包含政治敏感信息", "其他理由"];
            let PostId = this.data.topic.PostId;
            wx.showActionSheet({
                itemList: itemList,
                success: function(e) {
                    let options = {
                        id: PostId,
                        type: 1,
                        reason: e.tapIndex + 1
                    }
                    user.report(options)
                        .then(res => {
                            wx.showToast({
                                title: '举报成功',
                            })
                        })
                        .catch(err => {
                            wx.showToast({
                                title: err.msg,
                                icon: "none"
                            })
                        });
                }
            })
        },
        /**
         * 投票结果传递
         */
        vote(e){
          this.triggerEvent("vote",e.detail);
        },
        /**
         * 展示评论框
         */
        showCommentBox(){
            this.triggerEvent("showcommentbox",{
                pid:this.data.topic.PostId
            });
        }
    }
})